<?php
/*$user = 'longe10_haiweiya';
$pass = '1988sink';
try {
    $dbh = new PDO('mysql:host=127.0.0.1;dbname=doubanxia', $user, $pass);
    
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}*/

$dbms = 'mysql';//数据库类型 Oracle 用ODI,对于开发者来说，使用不同的数据库，只要改这个，不用记住那么多的函数了
$host = '127.0.0.1';//数据库主机名
$db_name = 'longe10_doubanxia';//使用的数据库
$user = 'longe10_douban';//数据库连接用户名
$pass = '1';//对应的密码
$db_charset = 'utf8';
$dsn="$dbms:host=$host;dbname=$db_name;charset=$db_charset";
class db extends PDO
{
    public function __construct()
    {
        try
        {
            parent::__construct("$GLOBALS[dsn]",$GLOBALS['user'],$GLOBALS['pass']);
            PDO::exec("SET NAMES 'utf8';");
        }
        catch(PDOException $e)
        {
        die("Error: ".$e->__toString()."<br/>");
        }
    }
    
    public final function query($sql)
    {
        try
        {
            return parent::query($this->setString($sql));
        }
        catch(PDOException $e)
        {
            die("Error: ".$e->__toString()."<br/>");
        }
    }
    
    private final function setString($sql)
    {
        //echo"我要处理一下$sql";
        return$sql;
    }
    
    public function implodeToColumn($data)
    {
		if($data=='*')
		{
			return '*';
		}
		if(is_array($data))
		{
			$a = implode('`,`',$data);
		}
		else
		{
			$a = $data;
		}
		$b = '`'.$a.'`';
		return $b;
    }
    
    public function implodeToValues($data)
    {
		if(is_array($data))
		{
			$a = implode("','",$data);
		}
		else
		{
			$a = $data;
		}
		$b = "'".$a."'";
		return $b;
    }
    
    public function implodeToWhere($data)
    {
    	$a = '1';
		if(is_array($data))
		{
			$a = '';
			foreach($data as $key=>$value)
			{
				if(empty($a))
				{
					$a = '`'.$key."`='".$value."' ";
				}
				else
				{
					$a .= ' AND `'.$key."`='".$value."' ";
				}
			}
		}
		return $a;
    }
    
    public function implodeToUpdate($data)
    {
		$a = '';
		foreach($data as $column=>$value)
		{
			if(empty($a))
			{
				$a = '`'.$column."`='".$value."'";
			}
			else
			{
				$a .= ',`'.$column."`='".$value."'";
			}
		}
		return $a;
    }
}
?>
